package br.com.ac.fatureweb.view;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.security.RolesAllowed;
import javax.faces.event.ActionEvent;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

import br.com.ac.fatureweb.enums.EnumProfileAction;
import br.com.ac.fatureweb.model.Perfil;
import br.com.ac.fatureweb.model.PerfilAcao;
import br.com.ac.fatureweb.service.NgcPerfilBean;
import br.com.ac.fatureweb.utilitary.Constants;
import br.com.ac.fatureweb.utilitary.CrudDefaultBean;
import br.com.ac.fatureweb.utilitary.EnumStatusManaged;

@Named
@ViewScoped
public class PerfilBean extends CrudDefaultBean<Perfil, NgcPerfilBean> {

	/**
	 *
	 */
	private static final long serialVersionUID = -5335159986601600944L;

	private List<Perfil> lstPerfil;

	@Inject
	private NgcPerfilBean ngcPerfilBean;

	private List<EnumProfileAction> lstProfileActionSelected;

	@PostConstruct
	public void inicialize() {
		setService(ngcPerfilBean);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @author Brunno Gonçalves (brunnogoncalves@gmail.com) 22/03/2015
	 *
	 * @see
	 * br.com.ac.fatureweb.utilitary.CrudDefaultBean#preparePersist(javax.faces
	 * .event.ActionEvent)
	 */
	@Override
	@RolesAllowed(Constants.PERMISSAO_INCLUIR)
	public void preparePersist(final ActionEvent event) {
		lstProfileActionSelected = new ArrayList<EnumProfileAction>();
		super.preparePersist(event);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @author Brunno Gonçalves (brunnogoncalves@gmail.com) 19/03/2015
	 *
	 * @see
	 * br.com.ac.fatureweb.utilitary.CrudDefaultBean#executeAfterShowWindowMerge
	 * ()
	 */
	@Override
	protected void executeAfterShowWindowMerge() {
		getEntity().setLstPerfilAcao(
				ngcPerfilBean.getLstPerfilAcao(getEntity()));
		lstProfileActionSelected = new ArrayList<EnumProfileAction>();
		if (getEntity().getLstPerfilAcao() != null) {
			for (PerfilAcao perfilAcao : getEntity().getLstPerfilAcao()) {
				lstProfileActionSelected.add(perfilAcao.getAcao());
			}
		}
	}

	/*
	 * (non-Javadoc)
	 *
	 * @author Brunno Gonçalves (brunnogoncalves@gmail.com) 19/03/2015
	 *
	 * @see
	 * br.com.ac.fatureweb.utilitary.CrudDefaultBean#runBeforeCallingService()
	 */
	@Override
	protected void runBeforeCallingService() {
		if (lstProfileActionSelected != null) {
			for (EnumProfileAction action : lstProfileActionSelected) {
				if (!verificarExistePerfilAcao(action)) {
					if (getEntity().getLstPerfilAcao() == null) {
						getEntity().setLstPerfilAcao(
								new ArrayList<PerfilAcao>());
					}
					getEntity().getLstPerfilAcao().add(
							new PerfilAcao(getEntity(), action));
				}
			}
		}
		removerPerfilAcaoNaoSelecionados();
	}

	/**
	 * @author Brunno Gonçalves (brunnogoncalves@gmail.com) 19/03/2015
	 * @param enumProfileAction
	 * @return
	 */
	private Boolean verificarExistePerfilAcao(
			EnumProfileAction enumProfileAction) {
		if (getEntity().getLstPerfilAcao() != null) {
			for (PerfilAcao pea : getEntity().getLstPerfilAcao()) {
				if (pea.getAcao().equals(enumProfileAction)) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * @author Brunno Gonçalves (brunnogoncalves@gmail.com) 19/03/2015
	 */
	private void removerPerfilAcaoNaoSelecionados() {
		List<PerfilAcao> lstRemover = new ArrayList<PerfilAcao>();
		for (PerfilAcao pea : getEntity().getLstPerfilAcao()) {
			Boolean existe = false;
			for (EnumProfileAction enumProfileAction : lstProfileActionSelected) {
				if (pea.getAcao().equals(enumProfileAction)) {
					existe = true;
					break;
				}
			}
			if (!existe) {
				lstRemover.add(pea);
			}
		}
		getEntity().setLstPerfilAcaoExcluir(lstRemover);
		getEntity().getLstPerfilAcao().removeAll(lstRemover);
	}

	public void adicionar(ActionEvent event) {
		try {
			if (lstPerfil == null) {
				lstPerfil = new ArrayList<Perfil>();
			}

			lstPerfil.add(new Perfil());

			setCurrentState(EnumStatusManaged.INCLUIR);

			// Thread.sleep(10000);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * @author Brunno Gonçalves (brunnogoncalves@gmail.com) 17/03/2015
	 * @return
	 */
	public List<EnumProfileAction> getLstOperacoes() {
		return Arrays.asList(EnumProfileAction.values());
	}

	public List<Perfil> getLstPerfil() {
		return lstPerfil;
	}

	public void setLstPerfil(List<Perfil> lstPerfil) {
		this.lstPerfil = lstPerfil;
	}

	public List<EnumProfileAction> getLstProfileActionSelected() {
		return lstProfileActionSelected;
	}

	public void setLstProfileActionSelected(
			List<EnumProfileAction> lstProfileActionSelected) {
		this.lstProfileActionSelected = lstProfileActionSelected;
	}

}
